package com.pool;

import java.util.concurrent.*;

//Executors  工具类、3大方法
//使用了线程池之后，使用线程池来创建线程
public class Demo01 {
    public static void main(String[] args) {
//        ExecutorService threadpool = Executors.newSingleThreadExecutor();//单个线程
//        ExecutorService threadpool = Executors.newFixedThreadPool(5);//创建一个固定的线程池的大小
//        ExecutorService threadpool = Executors.newCachedThreadPool();//可伸缩的，遇强则强，遇弱则弱

        System.out.println(Runtime.getRuntime().availableProcessors());

        ExecutorService threadpool = new ThreadPoolExecutor(
                2,5,3,
                TimeUnit.SECONDS,new LinkedBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy() //哪来的去哪里
        );
        try {
            for (int i = 0; i < 10; i++) {
                //使用了线程池之后，使用线程池来创建线程
                threadpool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"ok");
                });
            }

        } catch (Exception e){
            e.printStackTrace();
        } finally {
            //线程池用完，程序结束，关闭线程池
            threadpool.shutdown();
        }
    }
}
